package com.example.dt;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textview1);

        Button button5 = findViewById(R.id.btn5);
        Button button6 = findViewById(R.id.btn6);

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//               showDatePickerDialog();

                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this);
                datePickerDialog.setTitle("选择日期");

                datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker datePicker = datePickerDialog.getDatePicker();
                        int selectedYear = datePicker.getYear();
                        int selectedMonth = datePicker.getMonth()+1;
                        int selectedDay = datePicker.getDayOfMonth();
                        updateDateDisplay(selectedDay, selectedMonth, selectedYear);
                    }
                });
                datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"取消操作",Toast.LENGTH_SHORT).show();
                    }
                });
                datePickerDialog.show();
            }
        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        textView.setText(hourOfDay + ":" + minute );

                    }
                },17,4,true);

                timePickerDialog.show();

            }
        });
    }

    private void updateDateDisplay(int selectedDay, int selectedMonth, int selectedYear) {
        // 这里可以根据需要更新界面上的日期显示，例如更新一个 TextView 的文本内容
        textView.setText(selectedYear + "-" + selectedMonth + "-" + selectedDay);
    }
}